<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);

class c_ajax_class extends AWS_CONTROLLER
{
	var $per_page = 10;
	
	function setup()
	{
		HTTP::no_cache_header();
	}
	
	/**
	 * 列出受邀请的列表
	 */
	function invitation_list_action()
	{		
		$limit = $_GET["page"] * 1 * $this->per_page;
		
		$limit = $limit . ", {$this->per_page}";
		
		$invitation_list = $this->model('invitation')->get_invitation_list($this->user_id, $limit);
		
		if ($invitation_list)
		{
			$user_ids = array();

			foreach ($invitation_list as $key => $val)
			{
				if ($val['active_status'] == '1')
				{
					$user_ids[] = $val['active_uid'];
				}
			}

			$user_ids = array_unique($user_ids);
			
			if($user_ids)
			{
				if($user_info_tmp = $this->model('account')->get_users_by_uids($user_ids))
				{
	
					$users_infos = array();
		
					foreach ($user_info_tmp as $key => $val)
					{
						$user_data['user_name'] = $val['user_name'];
						$user_data['avatar_file'] = get_avatar_url($val['uid'], "mid");
						$users_infos[$val['uid']] = $user_data;
					}
		
					foreach ($invitation_list as $key => $val)
					{
						if ($val['active_status'] == '1')
						{
							$invitation_list[$key]['userinfo'] = $users_infos[$val['active_uid']];
						}
				}
				}
			}
		}
		
		TPL::assign("invitation_list", $invitation_list);
		
		TPL::output("invitation/ajax/invitation_list");
	}
	
	//执行邀请动作
	function invite_action()
	{
		$email = trim($_POST['email']);
		
		if (! H::isemail($email))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => ''
			), "-1", "请填写正确的邮箱"));
		}
		
		//判断当前用户是否还有邀请额
		if (! $this->model('account')->get_invitation_available($this->user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/invitation/')
			), "-1", "已经没有可使用的邀请名额"));
		}
		
		//搜索邮箱是否已为本站用户，存在则提示用户已存在，返回显示。
		

		if ($this->model('account')->check_email($email))
		{
			$user_info = $this->model('account')->get_users_by_email($email);
			
			$userinfo['uid'] = $user_info['uid'];
			
			$userinfo['real_name'] = $this->model('account')->get_real_name_by_uid($user_info['uid']);
			
			if ($user_info['uid'] == $this->user_id)
			{
				H::ajax_json_output(AWS_APP::RSM(null, "-3", "你不能邀请您自己"));
			}
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => '', 
				'userinfo' => $userinfo
			), "-2", "此邮箱已在本站注册帐号"));
		}
		
		//若再次填入已邀请过的邮箱，则再发送一次邀请邮件
		$invitation_info = $this->model('invitation')->get_invitation_by_email($email);
		
		if ($invitation_info)
		{
			if ($invitation_info['uid'] == $this->user_id)
			{				
				if ($this->model('invitation')->resend_invitation($invitation_info['invitation_id']))
				{
					H::ajax_json_output(AWS_APP::RSM(null, "-1", "重发邀请成功"));
				}
				else
				{
					H::ajax_json_output(AWS_APP::RSM(null, "-1", "邀请发送失败, 请联系管理员"));
				}
			}
			else
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'url' => '', 
					'userinfo' => $userinfo
				), "-2", "此邮箱已接收过本站发出的邀请"));
			}
		}
		
		//生成邀请码
		$invitation_code = $this->model('invitation')->get_unique_invitation_code($this->active_type);
		
		$invitation_id = $this->model('invitation')->add_invitation($this->user_id, $invitation_code, $email, time(), ip2long($_SERVER['REMOTE_ADDR']));
		
		if ($invitation_id)
		{
			$this->model('account')->edit_invitation_available($this->user_id, - 1);
			
			$this->model('email')->action_email(email_class::TYPE_INVITE_REG, $email, get_js_url("/account/register/email-" . urlencode($email) . "__icode-" . $invitation_code), array(
				'user_name' => $this->user_info['user_name'],
			));
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/invitation/')
			), "1", "邀请发送成功"));
		}
	}

	/**
	 * 重发邀请
	 */
	function invite_resend_action()
	{
		if ($this->model('invitation')->resend_invitation($_GET['invitation_id']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "重发邀请成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "邀请发送失败, 请联系管理员"));
		}
	}

	function invite_cancel_action()
	{
		$invitation_id = $_GET['invitation_id'] * 1;
		
		if (! $invitation_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "邀请 ID 不存在"));
		}
		
		$invitation_row = $this->model('invitation')->get_invitation_by_id($invitation_id);
		
		if (! $invitation_row)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => ''
			), "-1", "邀请信息不存在"));
		}
		
		//更新状态
		$this->model('invitation')->update_invitation_fields(array(
			"active_status" => "-1"
		), $invitation_id);
		
		if ($this->model('account')->edit_invitation_available($this->user_id, 1))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "操作成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "操作失败, 请联系管理员"));
		}
	}
}